<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-button type="primary" icon="el-icon-plus" @click="openAddDialog"
          >添加分类</el-button
        ></el-row
      >
      <tree-table
        class="tree-table"
        :data="cateList"
        :columns="cateColumns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
      >
        <template slot="status" slot-scope="scope">
          <i
            style="color: lightgreen"
            class="el-icon-success"
            v-if="scope.row.status === 1"
          ></i>
          <i class="el-icon-error" v-else></i>
        </template>

        <template slot="level" slot-scope="scope">
          <el-tag v-if="scope.row.level === 0" size="mini">一级</el-tag>
          <el-tag v-else-if="scope.row.level === 1" size="mini" type="success"
            >二级</el-tag
          >
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>

        <template slot="opt" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showEditDialog(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="delCate(scope.row.id)"
            >删除</el-button
          >
        </template>
      </tree-table>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="40%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="上级分类">
          <el-cascader
            style="width: 100%"
            v-model="selectedKeys"
            :options="cateOptions"
            :props="{
              expandTrigger: 'hover',
              value: 'id',
              label: 'name',
              children: 'children',
              checkStrictly: true,
            }"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="编辑分类" :visible.sync="editDialogVisible" width="40%">
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      cateList: [],
      cateOptions: [],
      selectedKeys: [],
      queryInfo: {
        pageNum: 1,
        pageSize: 15
      },
      addDialogVisible: false,
      editDialogVisible: false,
      addForm: {
        name: '',
        pid: 0,
        level: 0
      },
      editForm: {},
      addFormRules: {
        name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      editFormRules: {
        name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      cateColumns: [
        {
          label: '分类名称',
          prop: 'name'
        },
        {
          label: '是否有效',
          prop: 'status',
          type: 'template',
          template: 'status'
        },
        {
          label: '分类等级',
          prop: 'level',
          type: 'template',
          template: 'level'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ]
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    //获取分类列表
    async getCateList () {
      const { data: res } = await this.$http.get('api/goods/cates', {
        params: this.queryInfo
      })
      this.cateList = res.data.list
      console.log(res)
    },
    openAddDialog () {
      this.addDialogVisible = true
      this.getCateAll()
    },
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
      this.selectedKeys = []
      this.addForm.level = 0
      this.addForm.pid = 0
    },
    //获取一二级全部分类
    async getCateAll () {
      const { data: res } = await this.$http.get('api/goods/cate?type=2')
      this.cateOptions = res.data
    },
    handleChange (value) {
      if (value.length > 0) {
        this.addForm.pid = value[value.length - 1]
        this.addForm.level = value.length
      } else {
        this.addForm.pid = 0
        this.addForm.level = 0
      }
    },
    //添加操作
    addCate () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post(
          'api/goods/cates',
          this.addForm
        )
        if (res.errno !== 0) {
          this.$message.error('添加失败')
        }
        this.$message.success('添加成功')
        this.addDialogVisible = false
        this.getCateList()
      })
    },
    async showEditDialog (id) {
      const { data: res } = await this.$http.get(`api/goods/category?id=${id}`)
      if (res.errno !== 0) {
        return this.$message.error(res.errmsg)
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    async editCate () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put(
          'api/goods/cates',
          this.editForm
        )
        if (res.errno !== 0) {
          this.$message.error('编辑失败')
        }
        this.$message.success('编辑成功')
        this.editDialogVisible = false
        this.getCateList()
      })
    },
    async delCate (id) {
      const { data: res } = await this.$http.delete(
        `api/goods/cates?id=${id}`
      )
      console.log(res)
      if (res.errno !== 0) {
        return this.$message.error(res.errmsg)
      }
      this.$message.success('删除成功')
      this.getCateList()
    }
  }
}
</script>

<style lang="less" scoped>
.tree-table {
  margin-top: 15px;
}
</style
>
